import { renderPartTimeDuty } from '@/utils/utils';
import { useRequest } from '@/hooks';
import { createDictionary } from '@/dictionary';
import { getEmployeeInfo } from '@/pages/OnboardAndLeave/TransferPost/services';

// 讲组织路径转成 微微科技集团 / … / WORK / 格式
const getFinalOrganizationName = (fullOrganizationName = '') => {
    const nameArr = fullOrganizationName.split('/');
    if (nameArr.length < 4) {
        return fullOrganizationName;
    }
    nameArr.splice(1, nameArr.length - 3, '...');
    return nameArr.join(' / ');
};

const workNatureUnitMap = {
    full_time: '全职',
    part_time: '兼职',
    practice: '实习',
    consultant: '顾问',
    temporary: '临时工'
};

export const formatData = (data = {}) => {
    const [, getServiceStatusName] = createDictionary('SERVICE_STATUS_DICT');
    const [, getTryDurationUnitText] = createDictionary('TRY_DURATION_UNIT_DICT');
    const fullOrganizationName = data.organizationVOList?.[0]?.fullOrganizationName;
    return {
        ...data,
        fullOrganizationName,
        finalOrganizationName: getFinalOrganizationName(fullOrganizationName),
        partTimeDuty: renderPartTimeDuty(data.partTimeDutyAliasList),
        serviceStatusName: getServiceStatusName(data.serviceStatus),
        tryDurationText: `${data.tryDuration}${getTryDurationUnitText(Number(data.tryDurationUnit))}`,
        workNatureUnit: workNatureUnitMap[data.workNature]
    };
};

// 封装用户信息接口，各种格式化...
const useEmployeeData = (employeeCode, extra = {}) => {
    const { data = {}, loading } = useRequest(() => getEmployeeInfo({ employeeCode, ...extra }), {
        ready: !!employeeCode,
        refreshDeps: [employeeCode],
        formatResult: res => res?.data
    });

    return {
        loading,
        data: formatData(data)
    };
};

export default useEmployeeData;
